var app = getApp();
var request = app.request;
var common = require('../../../utils/common.js');
var WxParse = require('../../../utils/wxParse/wxParse.js');
var setting = app.globalData.setting;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBar: app.globalData.statusBar,
    url: app.globalData.setting.url,
    userInfo: null,
    isExpand: false,
    selectType: 1,
    danmuList: [{
        text: 'first',
        color: '#ff0000',
        time: 1
      },
      {
        text: 'second',
        color: '#008080',
        time: 2
      },
      {
        text: 'three',
        color: '#ff00ff',
        time: 3
      }
    ],
    resourceUrl: setting.resourceUrl,
    defaultAvatar: setting.resourceUrl + "/static/images/user68.jpg",
    data: null, //请求的商品详情数据
    result: null,
    content: '', //商品详情页html
    goodsAttrs: null, //商品属性列表
    cartGoodsNum: 0, //购物车商品数量
    specSelect: 0, //选中的组合规格数组spec_goods_price下标
    optionItemId: 0, //页面参数，页面初始化指定显示的itemid，用于活动
    goodsInputNum: 1, //选中的商品件数
    openSpecModal: false, //是否打开规格弹窗
    openPromModal: false, //是否打开优惠信息弹窗
    activeCategoryId: 0, //商品主页tab
    supportPageScroll: false, //微信版本是否支持页面滚动回顶部
    address: {
      address: '',
      district: 0,
    },
    shipping: '',
    shippingCost: 0,
    hasSpec: true,
    openPromxx: false,
    enterAddressPage: false,
    categories: [{
        name: "商品",
        id: 0
      },
      {
        name: "详情",
        id: 1
      },
      {
        name: "评论",
        id: 2
      }
    ],
    activeCategoryId2: 0, //商品内容tab
    categories2: [{
        name: "商品详情",
        id: 0
      },
      // { name: "规格参数", id: 1 },
    ],
    activeCategoryId3: 0, //商品评论tab
    categories3: [{
        name: "全部",
        id: 0,
        num: 0
      },
      {
        name: "好评",
        id: 1,
        num: 0
      },
      {
        name: "中评",
        id: 2,
        num: 0
      },
      {
        name: "差评",
        id: 3,
        num: 0
      },
      {
        name: "有图",
        id: 4,
        num: 0
      }
    ],
    select: { //选择的(规格)商品的参数，用于显示
      price: 0,
      stock: 0,
      specName: '',
      activity: null
    },
    timer: null, //活动倒计时定时器
    imChoose: 0, //0 QQ客服,1 IM客服,2 小能客服
    imgs: [], //商品图片组
    options: null,
    share_btn: false, //自定义分享按钮状态
    actionSheetHidden: true, //自定义actionSheet隐藏True
    activityIn: 0,
    isBragain: false,
    isSeparate: true,
    cardList: [],
    combinations: [], //搭配购
    ask_all: [], //问大家
    ask_number: [], //问答数
    good_id: "",
    maskShow: true,
    showCar: false,
    showYes: false,
    showStore: false,
    domHeight: 0,
    safeAreaBottom: 0,
    point_use_percent: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
    }, true);
    // if (options.scene) {
    //   var scene = decodeURIComponent(options.scene)
    //   var data = scene.split('&')
    //   options.goods_id = data[0].split('=')[1]
    //   options.item_id = data[1].split('=')[1]
    //   options.first_leader = data[2].split('=')[1]
    // }
    app.getConfig(function (res) {
      var im_choose = common.getConfigByName(res.config, 'im_choose');
      var guarantee = common.getConfigByName(res.config, 'auto_service_date');
      var point_rate = common.getConfigByName(res.config, 'point_rate');
      var point_use_percent = common.getConfigByName(res.config, 'point_use_percent', 'integral');
      that.setData({
        imChoose: im_choose,
        auto_service_date: guarantee,
        point_rate: point_rate,
        point_use_percent: point_use_percent
      });
    });
    that.data.optionItemId = typeof options.item_id == 'undefined' ? 0 : options.item_id;
    request.get('/api/goods/goodsInfo', {
      data: {
        id: options.goods_id
      },
      failRollback: true,
      isShowLoading: false,
      success: function (res) {
        if (res.data.result.goods.video != '' && res.data.result.goods.video != null) {
          res.data.result.goods.goods_images.unshift({
            goods_id: -1,
            image_url: res.data.result.goods.video,
            img_id: -1
          })
        }
        res.data.result.goods["minus_price"] = Math.floor((res.data.result.goods.market_price - res.data.result.goods.shop_price) * 10) / 10; // 已省价格
        console.log(res.data.result.goods);
        that.setData({
          result: res.data.result,
          data: res.data.result,
          good_id: options.goods_id
        });
        that.requestGoodsContent();
      }
    });
  },

  /** 请求商品详情页嵌入的html内容 */
  requestGoodsContent: function () {
    var that = this;
    request.get('/api/goods/goodsContent', {
      data: {
        id: this.data.data.goods.goods_id
      },
      // isShowLoading: false,
      success: function (res) {
        WxParse.wxParse('content', 'html', res.data.result.goods_content, that, 6);
        //网页中的图片加上域名
        common.wxParseAddFullImageUrl(that, 'content');
        that.setData({
          goodsAttrs: res.data.result.goods_attr_list
        });
      },
    });
  },

  selectBreakfast(e) {
    console.log(e)
    this.setData({
      selectType: e.currentTarget.dataset.index
    })
  },

  changeExpand() {
    this.setData({
      isExpand: !this.data.isExpand
    })
  },

  back() {
    wx.navigateBack();
  },

  /** 加入购物车 */
  addCart: function (e) {
    console.log('触发了点击事件')
    var that = this;
    var itemId = 0;
    var specs = this.data.data.goods.spec_goods_price;
    //区分有规格和无规格
    if (specs.length > 0) {
      if (specs[this.data.specSelect].store_count <= 0) {
        return app.showTextWarining("当前无库存！");
      }
      itemId = specs[this.data.specSelect].item_id;
    } else {
      if (this.data.data.goods.store_count <= 0) {
        return app.showTextWarining("当前无库存！");
      }
    }
    if (this.data.goodsInputNum <= 0) {
      return app.showTextWarining("商品数量不能小于1");
    }
    var data = {
      goods_id: this.data.data.goods.goods_id,
      goods_num: this.data.goodsInputNum,
      item_id: itemId,
      type: 2
    };
    // if (this.data.shippingCost < 0 || this.data.select.stock <= 0 || !this.data.hasSpec) {
    //   return;
    // }
    request.post('/api/cart/add', {
      data: data,
      success: function (res) {
        that.setData({
          openSpecModal: false
        });
        app.showTextWarining('加入购物车成功');
      }
    });
  },
})